// pages/cart/cart.js
var UTILS = require("../../utils/util.js");
var API = require("../../config/api.js");

var APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: -1,
    shopList: [],
    RESOURCE_ROOT: "",
    CHECKED: 1,
    imageURL: '//img.yzcdn.cn/upload_files/2017/07/02/af5b9f44deaeb68000d7e4a711160c53.jpg',
    checked: true
  },
  /**
   * 自定义函数 
   */
  changeChecked: function (event) {
    var _this = this;
    var dataset = event.currentTarget.dataset;
    var cartId = dataset.cartid;
    var checked = dataset.checked;
    var url = API.CHANGE_CART_CHECKED + "/" + cartId + "/" + (checked ? 0 : 1);
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };

    UTILS.httpGet(url, header, function(data) {
      if (data.result) {
        for (let i in _this.data.shopList) {
          for (let j in _this.data.shopList[i].carts) {
            if (_this.data.shopList[i].carts[j].id === cartId) {
              _this.setData({
                [`shopList[${i}].carts[${j}].checked`]: (checked ? 0 : 1)
              });
            }
          }
        }
      }
    });
  },
  getShopListInCartFromServer: function () {
    var _this = this;
    var userId = APP.globalData.userInfo.id;
    
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    var url;
    if (this.data.shopId < 0) {
      url = API.GET_SHOP_LIST_IN_CART + "/" + userId;
      UTILS.httpGet(url, header, function(data) {
        _this.setData({
          shopList: data.result
        });
        for (let index in data.result) {
          _this.getProductList(userId, data.result[index].id, function(carts) {
            _this.setData({
              [`shopList[${index}].carts`]: carts
            });
          });
        }
      });
    }
    
  },
  getProductList: function (userId, shopId, callback) {
    var _this = this;
    var url = API.GET_PRODUCT_LIST_IN_CART + "/" + userId + "/" + shopId;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(url, header, function(data) {
      callback(data.result);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shopId) {
      this.setData({
        shopId: options.shopId
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      RESOURCE_ROOT: API.RESOURCE_ROOT,
      CHECKED: UTILS.CHECKED
    });
    // this.getShopListInCartFromServer();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShopListInCartFromServer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})